﻿Imports CrystalDecisions.CrystalReports.Engine
Imports CrystalDecisions.Shared
Imports BUS
Imports DTO
Imports System.IO
Public Class BaoCaoTongKetMon

    Dim _rptbaocaotkm As rptBaoCaoTongKetMon

    Public Sub XuLy(ByVal _str As String, ByVal _giatri As String, ByVal _rptbaocaotkm As rptBaoCaoTongKetMon)
        Dim _crParameterFieldDefinitions As ParameterFieldDefinitions
        Dim _crParameterFieldDefinition As ParameterFieldDefinition
        Dim _crParameterValues As New ParameterValues()
        Dim _crParameterDiscreteValue As New ParameterDiscreteValue()
        _crParameterDiscreteValue.Value = _giatri ' lay gia tri tren giao dien
        _crParameterFieldDefinitions = _rptbaocaotkm.DataDefinition.ParameterFields
        _crParameterFieldDefinition = _crParameterFieldDefinitions(_str) 'lay Ten parameterfields
        _crParameterValues = _crParameterFieldDefinition.CurrentValues
        _crParameterValues.Clear()
        _crParameterValues.Add(_crParameterDiscreteValue)
        _crParameterFieldDefinition.ApplyCurrentValues(_crParameterValues)
    End Sub

    Private Sub btnxem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnxem.Click
        _rptbaocaotkm = New rptBaoCaoTongKetMon()
        Dim tbcurrent As CrystalDecisions.CrystalReports.Engine.Table
        Dim tlicurrent As CrystalDecisions.Shared.TableLogOnInfo
        For Each tbcurrent In _rptbaocaotkm.Database.Tables
            tlicurrent = tbcurrent.LogOnInfo
            With tlicurrent.ConnectionInfo
                .ServerName = Application.StartupPath + "\QuanLyHocSinh.mdb"
                '.UserID = ""
                '.Password = ""
                '.DatabaseName = ""
            End With
            tbcurrent.ApplyLogOnInfo(tlicurrent)
        Next tbcurrent
        Dim _MHdto As New CMonHocDTO()
        _MHdto = cbbmon.SelectedItem
        Dim _HKdto As New CHocKyDTO()
        _HKdto = cbbhocky.SelectedItem
        XuLy("MaMonHoc", _MHdto.MaMonHoc, _rptbaocaotkm)
        XuLy("TenHocKy", _HKdto.TenHocKy, _rptbaocaotkm)
        XuLy("TenMonHoc", _MHdto.TenMonHoc, _rptbaocaotkm)
        CrystalReportViewer1.ReportSource = _rptbaocaotkm
        CrystalReportViewer1.Refresh()
    End Sub

    Private Sub BaoCaoTongKetMon_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Dim _busmonhoc As New CMonHocBUS()
        cbbmon.DataSource = _busmonhoc.LayDanhSachMonHoc()
        cbbmon.DisplayMember = "TenMonHoc"
        cbbmon.ValueMember = "MaMonHoc"
        Dim _bushk As New CHocKyBUS()
        cbbhocky.DataSource = _bushk.LayDanhSachHK()
        cbbhocky.DisplayMember = "TenHocKy"
    End Sub

    Private Sub btnthoat_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnthoat.Click
        Me.Close()
    End Sub

    Private Sub btnexport_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnexport.Click
        Try
            Dim CrExportOptions As ExportOptions
            Dim CrDiskFileDestinationOptions As New DiskFileDestinationOptions()
            Dim CrFormatTypeOptions As New ExcelFormatOptions
            Dim _dlg As New SaveFileDialog()
            _dlg.Filter = "|*.xls||*.xlsx"

            If _dlg.ShowDialog() = Windows.Forms.DialogResult.OK Then
                CrDiskFileDestinationOptions.DiskFileName = _dlg.FileName
                CrExportOptions = _rptbaocaotkm.ExportOptions
                With CrExportOptions
                    .ExportDestinationType = ExportDestinationType.DiskFile
                    .ExportFormatType = ExportFormatType.Excel
                    .DestinationOptions = CrDiskFileDestinationOptions
                    .FormatOptions = CrFormatTypeOptions
                End With
                _rptbaocaotkm.Export()
            End If
        Catch ex As Exception
            MsgBox(ex.ToString)
        End Try
    End Sub
End Class